package com.primary.controller;

import com.primary.common.HttpCode;
import com.primary.common.MsgCode;
import com.primary.common.Result;
import com.primary.controller.dto.ChangePasswordDto;
import com.primary.controller.request.UsersRequest;
import com.primary.entity.Users;
import com.primary.service.UsersService;
import com.primary.utils.IpUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.ResourceBundle;

@RestController
@RequestMapping("/users")
public class UsersController {
    private static final ResourceBundle bundle;

    static {
        bundle = ResourceBundle.getBundle("config\\log_message");
    }
    @Resource
    UsersService usersService;

    @RequestMapping(value = "/allUsers", method = {RequestMethod.GET},produces = MediaType.APPLICATION_JSON_VALUE)
    public Result allUsers() {
        List<Users> list = usersService.allusers();
        return Result.success(list);
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/16 23:14 Sunday
     * @description When the browser requests this service, it should be checked all information could not be null
     *              then this controller will deliver this requesting to Service.
     * @return Result code, it should be “success” or “error”
     */
    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody Users users) {
        if (null != users.getName() &&
            null != users.getPassword() &&
            null != users.getAddress() &&
            null != users.getPhone() &&
            null != users.getEmail()) {
            return usersService.insertOneUser(users) > 0 ?
                        Result.success() : Result.error(HttpCode.HTTP_MINUS_114, bundle.getString(MsgCode.MSG10005));
        } else {
            return Result.error(bundle.getString(MsgCode.MSG10003));
        }
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/16 23:30 Sunday
     * @description When browser requests this controller, it will get an Object that is not null,
     *              and use it to update a users' information.
     * @updateAuthor Shen Jianhui
     * @updateDate 2023/07/22 Saturday
     * @updated Take into account when performing an updating operation, there is a situation that the username is existed,
     *          in this situation, if someone needs to update his/her password or username, it should use another interface.
     * @return Result code and message, “success” or “error”
     */
    @ResponseBody
    @RequestMapping(value = "/updateOne",method = {RequestMethod.PUT})
    public Result updateOneUser(@RequestBody Users users){
        if (null != users.getAddress() &&
            null != users.getPhone() &&
            null != users.getEmail()){
            return usersService.updateOneUserById(users) > 0 ?
                        Result.success() : Result.error("Updating is fail!");
        } else {
            return Result.error("Please Input All Information");
        }
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/16 23:44 Sunday
     * @description When browser requests the deleting controller, it will get an ID to be param,
     *              then check it and use it to delete a user.
     * @return Result code and message, “success” or “error”
     */
    @RequestMapping(value = "/deleteOne/{id}",method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id){
       return null == id ? Result.error("Id can not be null") :
               (usersService.deleteUserById(id) ? Result.success():Result.error("Deleting is fail"));
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/18 21:02 Tuesday
     * @description When browser requests the query by pages controller, it will get two default params, which are
     *              passed by the browser
     * @return Result code to browser
     */
    @RequestMapping(value = "/page",method = {RequestMethod.GET})
    public Result page(UsersRequest usersRequest){
        return Result.success(usersService.queryByPage(usersRequest));
    }

    /**
     * @author Shen Jianhui
     * @date 2023/10/22 17:17 Sunday
     * @description To provide a method that allowed a user change his/her password by himself/herself, or by admin.
     * @return Result code to browser
     */
    @RequestMapping(value = "changePwd", method = {RequestMethod.PUT})
    public Result changePwd(@RequestBody ChangePasswordDto users){
        return usersService.changePassword(users) > 0 ?
                Result.success():Result.error("校验信息不正确");
    }
    /**
     * @author Shen Jianhui
     * @date 2024/07/07 15:03 Sunday
     * @description To provide a method that allowed server to get users' ip address who is requesting server.
     * @return IP address
     */
    @RequestMapping("/getUserIP")
    public Result getUserIP(HttpServletRequest request){
        return null != request ?
                Result.success(IpUtil.getIpAddress(request)):Result.error(HttpCode.HTTP_MINUS_114);
    }
}
